package com.link.base.tencent.corpwx.externalcontact.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.base.tencent.corpwx.agent.util.AgentIdUtil;
import com.link.base.tencent.corpwx.externalcontact.dao.mybatis.mapper.UserBehaviorDataMapper;
import com.link.base.tencent.corpwx.externalcontact.model.UserBehaviorData;
import com.link.base.tencent.corpwx.externalcontact.util.ExternalContactUtil;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 联系客户统计数据 
 *
 * @author 欧阳振强
 * @date 2020-02-06 20:59:17.0
 */
@Service
public class UserBehaviorDataServiceImpl extends BasicServiceImpl<UserBehaviorData> implements UserBehaviorDataService {

    @Resource
    private UserBehaviorDataMapper userBehaviorDataMapper;
    @Resource
    private AgentService agentService;
    @Resource
    private KeyGenerateService keyGenerateService;
    
    @Override
    public BasicMapper<UserBehaviorData> getBasicMapper() {
        return userBehaviorDataMapper;
    }

    @Override
    public void generateUserBehaviorData() throws Exception {
        // 设置数据起始时间
        String startTime;
        UserBehaviorData param = new UserBehaviorData();
        UserBehaviorData userBehaviorData = userBehaviorDataMapper.getLastRecordByStatTime(param);
        if (userBehaviorData == null) {
            // 不存在现有数据，则默认查询30天内的
            startTime = String.valueOf(DateUtil.addDayToDate(new Date(), -30).getTime() / 1000);
        } else {
            String existStatTime = userBehaviorData.getStatTime();
            Date existStatTimeDate = DateUtil.stringToDate(existStatTime, "yyyy-MM-dd");
            Date startDate = DateUtil.addDayToDate(existStatTimeDate, 1);
            startTime = String.valueOf(startDate.getTime() / 1000);
        }
        // 设置数据结束时间
        String endTime = String.valueOf(DateUtil.addDayToDate(new Date(), -1).getTime() / 1000);
        String accessToken = agentService.getExistAccessToken(AgentIdUtil.CORPWX_ACCOUNT_AGENTID);
        // 直接查询根部门
        List<String> partyIdList = new ArrayList<>();
        partyIdList.add("1");
        JSONObject object = ExternalContactUtil.getUserBehaviorData(accessToken, null, partyIdList, startTime, endTime);
        JSONArray array = object.getJSONArray("behavior_data");
        for (int i = 0; i < array.size(); i++) {
            JSONObject item = array.getJSONObject(i);
            UserBehaviorData result = new UserBehaviorData();
            result.setId(keyGenerateService.keyGenerate());
            result.setStatTime(DateUtil.dateToStr(new Date(Long.valueOf(item.getString("stat_time") + "000")), "yyyy-MM-dd"));
            result.setNewApplyCnt(item.getString("new_apply_cnt"));
            result.setNewContactCnt(item.getString("new_contact_cnt"));
            result.setChatCnt(item.getString("chat_cnt"));
            result.setMessageCnt(item.getString("message_cnt"));
            result.setReplyPercentage(item.getString("reply_percentage"));
            result.setAvgReplyTime(item.getString("avg_reply_time"));
            result.setNegativeFeedbackCnt(item.getString("negative_feedback_cnt"));
            userBehaviorDataMapper.insert(result);
        }
    }
}
